<?php
if (!defined("OK") || (defined("LEVEL") && LEVEL < 1)) { header('location: ?'); }
if ($_SERVER['PHP_SELF'] == 'codebin.php') { header('location: ../'); }
# Duomenu paieska
# SELECT `pav` FROM `codebin` WHERE `pav` LIKE CONVERT(_utf8 '%te%' USING latin1) COLLATE latin1_swedish_ci

// Nustatom kintamuosius
if (isset($url['a']) && isnum($url['a']) && $url['a'] > 0) { $aid = ceil((int)$url['a']); }
if (isset($url['c']) && isnum($url['c']) && $url['c'] > 0) { $cid = ceil((int)$url['c']); }
if (isset($url['d']) && isnum($url['d']) && $url['d'] > 0) { $did = ceil((int)$url['d']); }
if (isset($url['p']) && isnum($url['p']) && $url['p'] > 0) { $p = ceil((int)$url['p']); } else { $p = 0; }
if (isset($url['o']) && isnum($url['o']) && $url['o'] > 0) { 
	if ($url['o'] == 0) { $order = "id DESC"; }
	elseif ($url['o'] == 1) { $order = "nick"; }
	elseif ($url['o'] == 2) { $order = "pav"; }
	elseif ($url['o'] == 3) { $order = "data DESC"; }
	elseif ($url['o'] == 4) { $order = "cat"; }
}
else { $order = "data DESC"; }
$limit = 30;
$viso = kiek('codebin');
// Naujo kodo iterpimo forma autohide
$code = '
<form name="code" action="" method="post">
	<input value="" type="input" name="title"/>
	<optgroup label="tipas">
	<label><input type="radio" name="cat" value="PHP" checked="checked" class="middle" /> PHP</label>
	<label><input type="radio" name="cat" value="mIRC" class="middle" /> mIRC</label>
	</optgroup>
	<textarea name="code" id="code" rows="10" style="width: 100%;" wrap="physical"></textarea><br/>
	<input value="Siųsti" type="submit"/>
	<input name="action" value="send" type="hidden"/>
</form>';
hide("Naujas kodas",$code,(($cid>0)?true:false),"Naujas kodas");

// Rodom koda
if (isset($cid) && $cid != 0) {
	$row = mysql_fetch_assoc(mysql_query1("SELECT * FROM `codebin` WHERE id=".escape($cid)." LIMIT 1"));
	$type = $row['cat'];
	$title = $row['nick']." (".$row['data'].") :: ".$row['pav'];
	$search = array("[php]","[/php]","[b]","[/b]","[mirc]","[/mirc]");
	$replace = array("","","","","","");
	$code = bbcode("[sm]".$row['nick']." (".$row['data'].")[/sm]\n[$type]".str_replace($search,$replace,$row['cod'])."[/$type]")."</span>";
	if (!empty($row['cod'])) { hide($title,$code); }
	else { klaida("Klaida","Kodas nerastas arba buvo ištrintas"); redirect("?id,7;p,$p","meta"); }
}

//Iterpiam nauja koda
if (isset($_POST) && !empty($_POST) && isset($_POST['cat']) && isset($_POST['code']) && !empty($_POST['code']) && isset($_POST['action']) && $_POST['action'] == 'send' && isset($_POST['title'])) { 
	if (isset($_SESSION['username'])) { $uzeris = $_SESSION['username']; } else { $uzeris = "Svečias"; }
	if (isset($_SESSION['id'])) { $uzer_id = $_SESSION['id']; } else { $uzer_id = 0; }
	mysql_query1("INSERT INTO `codebin` (`nick`, `nick_id`, `pav`, `cod`, `cat`, `data`) VALUES (".escape($uzeris).", ".escape($uzer_id).", ".escape(htmlspecialchars(strip_tags($_POST['title']))).", ".escape($_POST['code']).", ".escape($_POST['cat'])." , NOW( ))");
	if (mysql_affected_rows() > 0) { msg("Informacija","Naujas kodas buvo sėkmingai patalpintas"); redirect("?id,7;c,".mysql_insert_id()."","meta"); } else { klaida("Klaida","Jūsų kodas nebuvo patalpintas. <br>Patikrinkite ar teisingai užpildėte formą"); redirect("?id,7;p,$p","meta"); }
	unset($uzeris,$uzer_id);
}

//Trinam koda
if (isset($did) && $did != 0 && defined("LEVEL") && LEVEL > 20 && !isset($cid)) {
	mysql_query1("DELETE FROM `codebin` WHERE `id` = ".escape($did)." LIMIT 1");
	if (mysql_affected_rows() > 0) { msg("Kodas ištrintas","Kodas <b>$did</b> sėkmingai ištrintas."); redirect("?id,7;p,$p","meta"); } else { klaida("Klaida","Nurodytas kodas nebuvo ištrintas. Prašome patikrinti ar įvesti duomenys buvo teisingi"); redirect("?id,7;p,$p","meta"); }
	mysql_query1("DELETE FROM kom WHERE pid=7 AND kid=".escape($did)."");
}

// Rodom komentarus
if (isset($cid) && $cid > 0) {
	echo "<hr/>";
	include_once("priedai/komentarai.php");
	komentarai($cid);
}
else {
// Rodom kodus
$sql = mysql_query1("SELECT * FROM `codebin` ORDER BY $order LIMIT $p,$limit");
?>
<!-- CodeBin'as -->
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="0">
<tr class="title">
	<th><a href="<?=url("o,0")?>">Nr</a>: <?=(isset($url['o']) && $url['o'] == 0)?"↓":""?></th>
	<th><a href="<?=url("o,1")?>">Autorius</a>: <?=(isset($url['o']) && $url['o'] == 1)?"↓":""?></th>
	<th><a href="<?=url("o,2")?>">Pavadinimas</a>: <?=(isset($url['o']) && $url['o'] == 2)?"↓":""?></th>
	<th><a href="<?=url("o,3")?>">Info</a>: <?=(isset($url['o']) && $url['o'] == 3)?"↓":""?></th>
	<th><a href="<?=url("o,4")?>">Kategorija</a>: <?=(isset($url['o']) && $url['o'] == 4)?"↓":""?></th>
</tr>
<? while ($row = mysql_fetch_assoc($sql)) { ?>
<tr class="sarasas">
	<td><?=$row['id']?> <?=(defined("LEVEL") && LEVEL > 20)?'<a href="?id,7;d,'.$row['id'].'" onclick="return confirm(\'Ar tikrai norite trinti?\')"><img src="images/icons/cancel.png" class="middle" border=0 alt="x"/></a>':''?> <?=naujas($row['data'],$row['nick'])?></td>
	<td><?="<a href='?id,47;m,".$row['nick_id']."'>".$row['nick']."</a>"?></td>
	<td><a href="<?=url("c,".$row['id']."")?>"><?=$row['pav']?></a></td>
	<td align="center"><?=$row['data']?> </td>
	<td><img src="images/icons/<?=($row['cat']=='PHP'?'page_white_php.png':'page_white_code_red.png')?>" border="0" alt="<?=$row['cat']?>"/></td>
</tr>
<? } mysql_free_result($sql); ?>
</table>
<!-- CodeBino pabaiga -->
<?
echo "<br/>".puslapiai($p,$limit,$viso,10);
} //else
?>
